//index.js
//获取应用实例
const app = getApp()
var common = require('../../utils/common.js');
var isChoiceness = true;
Page({
  data: {
    title: '提现',
    integral:'',
    record:[],
    money:'',
    error: false,
    refresh: false,
    isFromSearch: true,
    searchLoading: false,
    searchLoadingComplete: false,
    callbackcount: 10,
    searchPageNum: 1,
    isIphoneX: false,
    statusBarHeight: app.globalData.statusBarHeight,
    recordsw: true,
    integralsw: false,
    tabNav:[
      {
        name: '收益记录',
        path: '/app/user/pointRecord'
      },
      {
        name: '提现记录',
        path: '/app/user/withdrawList'
      }
    ],
    currentTab: 0,
    incomeInfo: [],
    page:1,
    loading: true,
    wRecord: [],
    myInfo:[]
  },
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '京多多',
      path: 'pages/index/index?userId=' + app.globalData.userId,
      imageUrl: '/img/sharepic.jpg'
    }
  },
  onReachBottom: function () {
    this.setData({
      searchLoading: true,
      refresh: true
    });
    var that = this;
  },
  onLoad: function () {
    let isIphoneX = app.globalData.isIphoneX;
    this.setData({
      isIphoneX: isIphoneX
    })
    this.getuserinfoData()
    this.getrecorddata()
    this.getIncomeInfo()
  },
  getuserinfoData: function() {
    var that = this
    const requestTask = wx.request({
      method: "POST",
      header: { "content-type": "application/x-www-form-urlencoded" },
      data: {
        sessionId: app.globalData.sessionId
      },
      url: app.globalData.url + "/app/user/getInfo",
      async: true,
      timeout: 10000,
      success: function (data) {
        console.log(data)
        that.setData({
          myInfo: data.data.data,
          integral: data.data.data.point,
        });
      },
      error: function (textStatus) {
        console.error(textStatus);
      },
      complete: function (XMLHttpRequest, status) {
        if (status == 'timeout') {
          requestTask.abort()    // 超时后中断请求
          wx.showModal({
            title: '提示',
            content: '网络超时，请重试',
            showCancel: 'false',
            success: function (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '/pages/index/index'
                })
              }
            }
          })
        }
      }
    })
  },
  getrecorddata() {
    const that = this
    common.post({
      url: '/app/user/withdrawList', data: { sessionId: app.globalData.sessionId,pageNo: 1 }
    })
      .then((res) => {
        console.log(res.data)
        if (res.data.code == 0) {
          console.log(res.data)
          that.setData({
            wRecord: res.data.data
          })
          if(res.data.total < 11){
            that.setData({
              loading: false,
            })
          }
        }
      }).catch((res) => {
        console.log(res)
        app.timeOutTip()
      })
  },
  getrecorddataPage() {
    const that = this;
    var page = that.data.page + 1;

    common.post({ url: '/app/user/withdrawList', data: { sessionId: app.globalData.sessionId, pageNo: page } })
      .then((res) => {
        var data = that.data.wRecord;
        console.log(res.data)
        if (that.data.page * 10 < res.data.total) {
          that.setData({
            page: page
          });
          var postList = res.data.data;
          for (let i = 0; i < postList.length; i++) {
            let tmp = postList[i];
            data.push(tmp);
            console.log(data)
          }
          // that.setData({
          //   items: data,
          // })
          setTimeout(function () {
            that.setData({
              wRecord: data,
              more: true
            });
            console.log(data)
          }, 300)
        } else {
          console.log('没有了')
          wx.showToast({
            title: '没有了',
            icon: 'none',
            duration: 2000
          })
          that.setData({
            loading: false,
          })
        }

      }).catch((res) => {
        console.log(res)

      })
  },
  getintegraldata: function () {
    var that = this
    const requestTask = wx.request({
      method: "POST",
      header: { "content-type": "application/x-www-form-urlencoded" },
      data: {
        pageNo: that.data.searchPageNum,
        sessionId: app.globalData.sessionId
      },
      url: app.globalData.url + "/app/user/pointRecord",
      async: true,
      timeout: 10000,
      success: function (data) {
        console.log(data)
        that.setData({
          record: data.data.data,
          recordsw: false,
          integralsw: true,
          searchPageNum: 1
        });
        console.log(that.data.record)
      },
      error: function (textStatus) {
        console.error(textStatus);
      },
      complete: function (XMLHttpRequest, status) {
        if (status == 'timeout') {
          requestTask.abort()    // 超时后中断请求
          wx.showModal({
            title: '提示',
            content: '网络超时，请重试',
            showCancel: 'false',
            success: function (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '/pages/index/index'
                })
              }
            }
          })
        }
      }
    })
  },
  exchange: function () {
    if(!this.data.error){
      wx.showLoading({
        title: '提交中...',
        mask: 'true',
      })
      var that = this
      const requestTask = wx.request({
        method: "POST",
        header: { "content-type": "application/x-www-form-urlencoded" },
        data: {
          // money: that.data.integral,
          sessionId: app.globalData.sessionId,
          appId: 'wx0f394e57b45e656e'
        },
        url: app.globalData.url + "/app/user/withdraw",
        async: true,
        timeout: 10000,
        success: function (data) {
          wx.hideLoading()
          console.log(data)
          if (data.data.code == 0) {
            that.setData({
              money: '',
            });
            wx.showToast({
              title: '已经申请提现',
              icon: 'success',
              duration: 3000
            })
            that.getuserinfoData()
            that.getrecorddata()
          } else {
            wx.showToast({
              title: data.data.errmsg,
              icon: 'none',
              duration: 3000
            })
          }
        },
        error: function (textStatus) {
          console.error(textStatus);
        },
        complete: function (XMLHttpRequest, status) {
          if (status == 'timeout') {
            requestTask.abort()    // 超时后中断请求
            wx.showModal({
              title: '提示',
              content: '网络超时，请重试',
              showCancel: 'false',
              success: function (res) {
                if (res.confirm) {
                  wx.redirectTo({
                    url: '/pages/index/index'
                  })
                }
              }
            })
          }
        }
      })
    }
  },
  extract:function (){
    this.setData({
      money: this.data.integral,
    });
  },
  inputBox: function (e) {
    if (e.detail.value < 0.3){
      this.setData({
        error: true,
      });
    }else if (e.detail.value > this.data.integral){
      this.setData({
        error: true,
      });
    }else{
      this.setData({
        error: false,
        money: e.detail.value,
      });
    }
  },
  searchScrollLower: function (e) {
    console.log(e)
    var that = this;
    if (that.data.searchLoading && !that.data.searchLoadingComplete) {
      that.setData({
        searchPageNum: that.data.searchPageNum + 1,
        isFromSearch: false  //触发到上拉事件，把isFromSearch设为为false
      });
      var searchPageNum = that.data.searchPageNum,//把第几次加载次数作为参数  
        callbackcount = that.data.callbackcount; //返回数据的个数
      isChoiceness = true
      var thisurl
      if (that.data.recordsw){
        thisurl = '/app/user/withdrawList'
      }else{
        thisurl = '/app/user/pointRecord'
      }
      const requestTask = wx.request({
        method: "POST",
        header: { "content-type": "application/x-www-form-urlencoded" },
        data: {
          pageNo: that.data.searchPageNum,
          sessionId: app.globalData.sessionId
        },
        url: app.globalData.url + thisurl,
        async: true,
        timeout: 10000,
        success: function (data) {
          console.log(data)
          console.log(that.data.searchPageNum)
          var num = 10 * (that.data.searchPageNum - 1)
          if (data.data.total > num) {
            var items = that.data.record.concat(data.data.data)
            console.log(items)
            that.setData({
              record: items, //获取数据数组  
              searchLoading: true   //把"上拉加载"的变量设为false，显示  
            });
            console.log(that.data.record)
          } else {
            that.setData({
              searchLoading: false  //把"上拉加载"的变量设为false，隐藏
            });
            wx.showToast({
              title: '没有更多商品了',
              icon: 'none',
              duration: 2000
            });
          }
        },
        error: function (textStatus) {
          console.error(textStatus);
        },
        complete: function (XMLHttpRequest, status) {
          if (status == 'timeout') {
            requestTask.abort()    // 超时后中断请求
            wx.showModal({
              title: '提示',
              content: '网络超时，请重试',
              showCancel: 'false',
              success: function (res) {
                if (res.confirm) {
                  wx.redirectTo({
                    url: '/pages/index/index'
                  })
                }
              }
            })
          }
        }
      })
    } else {
      console.log(that.data.searchLoading, that.data.searchLoadingComplete)
    }
  },
  back: function () {
    wx.redirectTo({
      url: '/pages/usercenter/usercenter'
    })
  },
  toHelp(){
    wx.navigateTo({
      url: '/pages/help3/help3',
      success: function (res) {
        console.log('toHelp3')
      },
      fail: function (res) {
        console.log(res)
      }
    })
  },
  tabChange(e){
    console.log(e)
    var num = e.currentTarget.dataset.index
    this.setData({
      currentTab: num
    })
    if(num == 0){
      this.getIncomeInfo()
    }else if(num == 1){
      this.getrecorddata()
    }
  },
  getIncomeInfo(){
    const that = this
    common.post({
      url: '/app/user/myIncomeInfo', data: { sessionId: app.globalData.sessionId }
    })
      .then((res) => {
        console.log(res.data)
        if (res.data.code == 0) {
          console.log(res.data)
          that.setData({
            incomeInfo: res.data.data
          })
        }
      }).catch((res) => {
        console.log(res)
        app.timeOutTip()
      })
  },
  lvup(){
    wx.switchTab({
      url: '/pages/myStore/myStore',
      success: function (res) {
        console.log('toMyStore')
      },
      fail: function (res) {
        console.log(res)
      }
    })
  }
})
